package br.com.wepa.webapps.orca.controle.actions.usuario;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import br.com.wepa.webapps.logger.TraceLogger;
import br.com.wepa.webapps.orca.controle.ControleWeb;
import br.com.wepa.webapps.orca.controle.actions.struts.ActionEvent;
import br.com.wepa.webapps.orca.controle.actions.struts.ActionTracer;
import br.com.wepa.webapps.orca.controle.actions.struts.GenericAction;
import br.com.wepa.webapps.orca.logica.negocio.facade.FacadeDelegator;
import br.com.wepa.webapps.orca.logica.negocio.facade.basic.GenericFacadeHome;
import br.com.wepa.webapps.orca.logica.negocio.facade.basic.GenericTO;

public class UsuarioAction extends GenericAction{
	
	
    /**
     * Objeto de Log
     */    
	private static TraceLogger logger  = new TraceLogger(UsuarioAction.class);

	public UsuarioAction(){
		super();
	}

	
	@Override
	protected GenericFacadeHome getFacade() {
		return FacadeDelegator.getUsuarioFacade();
	}
	

	@Override
	public ActionForward gravar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {	
		ActionForward forward;
		try{					
			forward = super.gravar(mapping, form, request, response);
		}catch (LoginIndisponivelException e) {
			super.addError(request, ERROR_lOGIN_INDISPONIVEL);			
			ActionTracer tracer = getActionTracer(request);
			ActionEvent event = tracer.getLast();	
			GenericTO to = getGenericTansferObjectInRequest(request);
			super.copyFormToObject(to, form);
			if(event.getForward().getPath().indexOf("goCadastrar") != -1){
				forward = super.goCadastrar(mapping, form, request, response);				
			}else{
				forward = super.goAltualizar(mapping, form, request, response);
			}
			super.copyObjectToForm(form,to);
			ControleWeb.setInScope(event.getName(), form, request,event.getScope());
		}
		return forward;
	}
}